package com.valdemar.demos;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

import com.google.web.bindery.event.shared.SimpleEventBus;
import com.google.web.bindery.requestfactory.shared.RequestFactory;
import com.google.web.bindery.requestfactory.vm.RequestFactorySource;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

public class Util
{

	/**
	 * Tag for logging.
	 */
	private static final String TAG = "Util";

	// Shared constants

	/**
	 * Key for account name in shared preferences.
	 */
	public static final String ACCOUNT_NAME = "accountName";

	/**
	 * Key for auth cookie name in shared preferences.
	 */
	public static final String AUTH_COOKIE = "authCookie";

	/**
	 * Key for connection status in shared preferences.
	 */
	public static final String CONNECTION_STATUS = "connectionStatus";

	/**
	 * Value for {@link #CONNECTION_STATUS} key.
	 */
	public static final String CONNECTED = "connected";

	/**
	 * Value for {@link #CONNECTION_STATUS} key.
	 */
	public static final String CONNECTING = "connecting";

	/**
	 * Value for {@link #CONNECTION_STATUS} key.
	 */
	public static final String DISCONNECTED = "disconnected";

	/**
	 * Key for device registration id in shared preferences.
	 */
	public static final String DEVICE_REGISTRATION_ID = "deviceRegistrationID";

	/**
	 * Key for shared preferences.
	 */
	private static final String SHARED_PREFS = "teste".toUpperCase(Locale.ENGLISH) + "_PREFS";

	
	 /*
     * URL suffix for the RequestFactory servlet.
     */
    public static final String RF_METHOD = "/gwtRequest";

    
	/**
	 * Helper method to get a SharedPreferences instance.
	 */
	public static SharedPreferences getSharedPreferences(Context context)
	{
		return context.getSharedPreferences(SHARED_PREFS, 0);
	}

	/**
     * Cache containing the base URL for a given context.
     */
    private static final Map<Context, String> URL_MAP = new HashMap<Context, String>();

    
	/**
	 * Returns the (debug or production) URL associated with the registration service.
	 */
	public static String getBaseUrl(Context context)
	{
		String url = URL_MAP.get(context);
		if (url == null)
		{
			url = Setup.PROD_URL;
			URL_MAP.put(context, url);
		}
		return url;
	}

	/**
	 * Creates and returns an initialized {@link RequestFactory} of the given type.
	 */
	public static <T extends RequestFactory> T getRequestFactory(Context context,
			Class<T> factoryClass)
	{
		T requestFactory = RequestFactorySource.create(factoryClass);

		SharedPreferences prefs = getSharedPreferences(context);
		String authCookie = prefs.getString(Util.AUTH_COOKIE, null);

		String uriString = Util.getBaseUrl(context) + RF_METHOD;
		URI uri;
		try
		{
			uri = new URI(uriString);
		}
		catch (URISyntaxException e)
		{
			Log.w(TAG, "Bad URI: " + uriString, e);
			return null;
		}
		requestFactory.initialize(new SimpleEventBus(),
				new AndroidRequestTransport(uri, authCookie));

		return requestFactory;
	}

}
